/*
 * This code belongs to
 * Krishna Brahmam, Dept. of CSE, IIT Guwahati
 */

package speech;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author  Krishna Brahmam
 */
public class TestingGUI extends javax.swing.JFrame {
    private MainGUI parent;
    private String[] output;
    
    /** Creates new form TrainingGUI */
    public TestingGUI(MainGUI parent) {
        this.parent = parent;
        initComponents();
        
        order.setText(parent.getOrder()+"");
        frameLength.setText(parent.getFrameLength()+"");
        frameShift.setText(parent.getFrameShift()+"");
        codebookSize.setText(parent.getCodebookSize()+"");
        states.setText(parent.getStates()+"");
        
        testDirectory.setText(parent.getTestDirectory());
        
        outputWordlist.setText(parent.getOutputWordlist());
        indexFileName.setText(parent.getIndexFileName());
        listFileName.setText(parent.getTestListFileName());
        codebookFileName.setText(parent.getCodebookFileName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        indexFileName = new javax.swing.JTextField();
        indexButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        listFileName = new javax.swing.JTextField();
        listButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        codebookFileName = new javax.swing.JTextField();
        codebookButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        outputWordlist = new javax.swing.JTextField();
        wordlistButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        testDirectory = new javax.swing.JTextField();
        testSamplesButton = new javax.swing.JButton();
        testButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        order = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        frameLength = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        frameShift = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        codebookSize = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        states = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Testing");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Testing"));

        jLabel1.setText("Index file");

        indexFileName.setEditable(false);

        indexButton.setText("Browse");
        indexButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                indexButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("List");

        listFileName.setEditable(false);

        listButton.setText("Browse");
        listButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Codebook");

        codebookFileName.setEditable(false);

        codebookButton.setText("Browse");
        codebookButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codebookButtonActionPerformed(evt);
            }
        });

        jLabel4.setText("Output wordlist");

        outputWordlist.setEditable(false);

        wordlistButton.setText("Browse");
        wordlistButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wordlistButtonActionPerformed(evt);
            }
        });

        jLabel5.setText("Test samples");

        testDirectory.setEditable(false);

        testSamplesButton.setText("Browse");
        testSamplesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testSamplesButtonActionPerformed(evt);
            }
        });

        testButton.setText("Test");
        testButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testButtonActionPerformed(evt);
            }
        });

        jLabel6.setText("LPC Order");

        order.setEditable(false);

        jLabel7.setText("Frame Length");

        frameLength.setEditable(false);

        jLabel8.setText("Frame Shift");

        frameShift.setEditable(false);

        jLabel9.setText("Codebook Size");

        codebookSize.setEditable(false);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel10.setText("# HMM States");

        states.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(236, 236, 236))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jLabel3)
                                                    .addComponent(jLabel2)
                                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(32, 32, 32)
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(indexFileName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(listFileName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(codebookFileName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(outputWordlist, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(testDirectory, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(frameLength, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(order, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(frameShift, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(codebookSize, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                                    .addComponent(states, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel5)
                                                .addGap(195, 195, 195))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel4)
                                                .addGap(216, 216, 216)))
                                        .addGap(18, 18, 18))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(testButton)
                                        .addGap(8, 8, 8)))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(indexButton)
                                    .addComponent(listButton)
                                    .addComponent(codebookButton)
                                    .addComponent(wordlistButton)
                                    .addComponent(cancelButton)
                                    .addComponent(testSamplesButton))))
                        .addGap(73, 73, 73))
                    .addComponent(jLabel10))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(indexFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(indexButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(listFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(listButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(codebookFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(codebookButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(outputWordlist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(wordlistButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(testDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(testSamplesButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(order, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(frameLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(frameShift, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(codebookSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(states, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(testButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void indexButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_indexButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        indexFileName.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_indexButtonActionPerformed

private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        listFileName.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_listButtonActionPerformed

private void codebookButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codebookButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        codebookFileName.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_codebookButtonActionPerformed

private void wordlistButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wordlistButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        outputWordlist.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_wordlistButtonActionPerformed

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
    if( checkFields()){
        parent.setTestFileNames(listFileName.getText(),outputWordlist.getText(), testDirectory.getText());
        Main m = new Main(parent.getOrder(), parent.getCodebookSize(),
                          parent.getFrameLength(), parent.getFrameShift(),
                          parent.getStates(), null);
        extractWordlist(outputWordlist.getText());
        m.test(listFileName.getText(), testDirectory.getText(), 
               codebookFileName.getText(), indexFileName.getText(), output);
        // Show results
        JOptionPane.showMessageDialog(this, "Finished testing", "Status report", JOptionPane.INFORMATION_MESSAGE);
        dispose();
    }
    else{
        JOptionPane.showMessageDialog(this, "Please fill in all details", "Invalid details", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_testButtonActionPerformed

private void testSamplesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testSamplesButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        testDirectory.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_testSamplesButtonActionPerformed

private boolean checkFields(){
    if(!indexFileName.getText().equals("") 
            && !listFileName.getText().equals("")
            && !codebookFileName.getText().equals("") 
            && !order.getText().equals("")
            && !outputWordlist.getText().equals("")
            && !frameLength.getText().equals("") 
            && !frameShift.getText().equals("")
            && !codebookSize.getText().equals("")
            && !states.getText().equals("")){
        return true;
    }
    return false;
}

private void extractWordlist(String filename){
    int i;
    try {
        Scanner s = new Scanner(new File(filename)).useDelimiter("\\n");
        i = 0;
        while(s.hasNext()){
            s.next();
            i++;
        }
        output = new String[i];
        i = 0; s.close();
        s = new Scanner(new File(filename)).useDelimiter("\\n");
        while(s.hasNext()){
            output[i] = new String(s.next().trim());
            i++;
        }
        s.close();
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        dispose();
    }
}
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton codebookButton;
    private javax.swing.JTextField codebookFileName;
    private javax.swing.JTextField codebookSize;
    private javax.swing.JTextField frameLength;
    private javax.swing.JTextField frameShift;
    private javax.swing.JButton indexButton;
    private javax.swing.JTextField indexFileName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton listButton;
    private javax.swing.JTextField listFileName;
    private javax.swing.JTextField order;
    private javax.swing.JTextField outputWordlist;
    private javax.swing.JTextField states;
    private javax.swing.JButton testButton;
    private javax.swing.JTextField testDirectory;
    private javax.swing.JButton testSamplesButton;
    private javax.swing.JButton wordlistButton;
    // End of variables declaration//GEN-END:variables

}
